using System;
using System.Data;
using System.Data.Common;
using System.Data.SqlTypes;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BaHuan.Common;
using BaHuan.Entities;
/*
 * For each DB unit, normally tables, we create a DAO class for it
 * this will enalbe us to do some basic tasks for a DB unit
 * such as Save, Delete, GetAll, GetById....
 * 
 * */
namespace BaHuan.DataLayer
{
	
	class DoiTruThueDAO:BaseDAO
	{
	
		// save into DB, @id = -1: insert else, update
		public ResultObj SaveDoiTruThue(DoiTruThue doitruthue)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			SqlConnection conObj = DataConnection.GetSqlConnection();
			SqlCommand cmd = CreateSqlCommand("spAddDoiTruThue", CommandType.StoredProcedure);
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.CommandText = "spAddDoiTruThue";
			SqlParameter param = new SqlParameter();
			param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, doitruthue.Id);
			cmd.Parameters.Add(param);
            param = CreateParameter("@Prefix", DbType.String, ParameterDirection.Input, doitruthue.Prefix);
            cmd.Parameters.Add(param);
            param = CreateParameter("@OrderNumber", DbType.String, ParameterDirection.Input, doitruthue.OrderNumber);
            cmd.Parameters.Add(param);
            param = CreateParameter("@MaSo", DbType.String, ParameterDirection.Input, doitruthue.MaSo);
            cmd.Parameters.Add(param);
            param = CreateParameter("@History", DbType.String, ParameterDirection.Input, doitruthue.History);
            cmd.Parameters.Add(param);
			param = CreateParameter("@NgayThang", DbType.DateTime, ParameterDirection.Input, doitruthue.NgayThang);
			cmd.Parameters.Add(param);
			param = CreateParameter("@DienGiai", DbType.String, ParameterDirection.Input, doitruthue.DienGiai);
			cmd.Parameters.Add(param);
			param = CreateParameter("@ThueGTGTDuocKhauTru1331", DbType.Int64, ParameterDirection.Input, doitruthue.ThueGTGTDuocKhauTru1331);
			cmd.Parameters.Add(param);
			param = CreateParameter("@ThueGTGTDauRa3331", DbType.Int64, ParameterDirection.Input, doitruthue.ThueGTGTDauRa3331);
			cmd.Parameters.Add(param);
			param = CreateParameter("@TienKhauTru", DbType.Int64, ParameterDirection.Input, doitruthue.TienKhauTru);
			cmd.Parameters.Add(param);
			param = CreateParameter("@ReturnVal", DbType.String, ParameterDirection.ReturnValue, result.ReturnCode);
			cmd.Parameters.Add(param);
			conObj.Open();
			cmd.ExecuteNonQuery();
			conObj.Close();
			result.ReturnCode = Convert.ToInt32(cmd.Parameters["@ReturnVal"].Value);
			
			return result;
		}
		
		// Get all DoiTruThue in DB
		public ResultObj GetAllDoiTruThue( )
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<DoiTruThue> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetAllDoiTruThue", CommandType.StoredProcedure);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<DoiTruThue>();
			DoiTruThue item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new DoiTruThue();
			item.Id = Convert.ToInt32(reader["Id"]);
            item.Prefix = Convert.ToString(reader["Prefix"]);
            item.OrderNumber = Convert.ToString(reader["OrderNumber"]);
            item.MaSo = Convert.ToString(reader["MaSo"]);
            item.History = Convert.ToString(reader["History"]);
			item.NgayThang = Convert.ToDateTime(reader["NgayThang"]);
			item.DienGiai = Convert.ToString(reader["DienGiai"]);
			item.ThueGTGTDuocKhauTru1331 = Convert.ToInt64(reader["ThueGTGTDuocKhauTru1331"]);
			item.ThueGTGTDauRa3331 = Convert.ToInt64(reader["ThueGTGTDauRa3331"]);
			item.TienKhauTru = Convert.ToInt64(reader["TienKhauTru"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}
		
		// Get all DoiTruThue in DB with condition
		public ResultObj GetAllDoiTruThueByID(DoiTruThue doitruthue)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<DoiTruThue> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetAllDoiTruThueByID", CommandType.StoredProcedure);
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, doitruthue.Id);
			cmd.Parameters.Add(param);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<DoiTruThue>();
			DoiTruThue item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new DoiTruThue();
			item.Id = Convert.ToInt32(reader["Id"]);
            item.Prefix = Convert.ToString(reader["Prefix"]);
            item.OrderNumber = Convert.ToString(reader["OrderNumber"]);
            item.MaSo = Convert.ToString(reader["MaSo"]);
            item.History = Convert.ToString(reader["History"]);
			item.NgayThang = Convert.ToDateTime(reader["NgayThang"]);
			item.DienGiai = Convert.ToString(reader["DienGiai"]);
			item.ThueGTGTDuocKhauTru1331 = Convert.ToInt64(reader["ThueGTGTDuocKhauTru1331"]);
			item.ThueGTGTDauRa3331 = Convert.ToInt64(reader["ThueGTGTDauRa3331"]);
			item.TienKhauTru = Convert.ToInt64(reader["TienKhauTru"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}
		
		// Delete a DoiTruThue out of DB with condition
		public ResultObj DeleteDoiTruThue(DoiTruThue doitruthue)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			SqlConnection conObj = DataConnection.GetSqlConnection();
			SqlCommand cmd = CreateSqlCommand("spDeleteDoiTruThue", CommandType.StoredProcedure);
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.CommandText = "spDeleteDoiTruThue";
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, doitruthue.Id);
			cmd.Parameters.Add(param);
			conObj.Open();
			result.ReturnCode = cmd.ExecuteNonQuery();
			conObj.Close();
			
			return result;
		}
					
	}
}
